import React, { useState, useEffect } from 'react';
import { g_wsn } from 'db/state';
import { ck_float, IkFind, ck_empty, IkInput, useForm, obj_float, d_sort, obj_str, uDb, cline, Wait, NuiDlg } from 'sui';

const DlgPair = ({ open, setOpen, cur, hcmd }) => {
    const [wait, setWait] = useState(false);
    const [lst_tech, lstTech] = useState(false);
    const [value, setValue] = useState();
    const form = useForm(setValue);

    useEffect(() => {
        if (!open) {
            return;
        }

        if (cur) {
            setValue(cur);
        } else {
            setValue({
                diff: 1,
                w: 0.5,
                range: 1,
            });
        }

        uDb(null, "tech/list", { "line": cline() },
            r => {
                const ret = [];
                r.forEach(d => {
                    ret.push({ id: d.sn, sn: d.sn, name: d.name, ball: d.ball, num: d.num, mt: d.mt });
                });

                d_sort(ret, "sn");
                lstTech(ret);
            }, e => lstTech(null));
    }, [open, cur]);

    const hdone = () => {
        const ck = {
            tsn: v => ck_empty(v, "工艺"),
            diff: v => ck_float(v, "直径差"),
            w: v => ck_float(v, "宽度差"),
            base: v => ck_float(v, "调整值"),
            range: v => ck_float(v, "误差值"),
            dmin: v => ck_float(v, "最小值"),
            dmax: v => ck_float(v, "最大值"),
            kmin: v => ck_float(v, "最小值"),
            kmax: v => ck_float(v, "最大值"),
        };

        if (!form.hcheck(ck, value)) {
            return;
        }

        setWait(true);
        const wsn = g_wsn();
        const tsn = obj_str(value, "tsn");

        const tech = lst_tech.find(d => d.sn === tsn);

        uDb(null, `file/md/select_${wsn}`,
            {
                line: cline(),
                sn: "pair",
                v: {
                    gc: true,
                    tsn: tsn,
                    diff: obj_float(value, "diff"),
                    w: obj_float(value, "w"),
                    base: obj_float(value, "base"),
                    range: obj_float(value, "range"),
                    dmin: obj_float(value, "dmin"),
                    dmax: obj_float(value, "dmax"),
                    kmin: obj_float(value, "kmin"),
                    kmax: obj_float(value, "kmax"),
                    ball: obj_float(tech, "ball"),
                    num: obj_float(tech, "num"),
                    mt: obj_str(tech, "mt"),
                }
            },
            r => {
                setWait(false);
                setOpen(false);
                hcmd()
            }, e => {
                setWait(false);
                setOpen(false);
                alert("修改失败");
            });
    };

    return (
        <NuiDlg open={open} title="配对参数" hcmd={hdone} labd="确定" hcancel={() => setOpen(false)}>
            <div className="flex flex-col my-2">
                <IkFind label="工艺" value={value} form={form} name="tsn" lst={lst_tech}/>
                <IkInput label="直径差" value={value} form={form} name="diff"/>
                <IkInput label="宽度差" value={value} form={form} name="w"/>
                <div className="inline-flex items-center">
                    <span className="text-base mr-4 mt-1">游隙</span>
                    <IkInput label="调整值" value={value} form={form} name="base" width="w-40"/>
                    <span className="text-base mx-4">{"+/-"}</span>
                    <IkInput label="误差值" value={value} form={form} name="range" width="w-40"/>
                </div>
                <div className="inline-flex items-center">
                    <span className="text-base mr-4 mt-1">外圆范围</span>
                    <IkInput label="最小值" value={value} form={form} name="dmin" className="mr-4" width="w-40"/>
                    <IkInput label="最大值" value={value} form={form} name="dmax" width="w-40"/>
                </div>
                <div className="inline-flex items-center">
                    <span className="text-base mr-4 mt-1">内孔范围</span>
                    <IkInput label="最小值" value={value} form={form} name="kmin" className="mr-4" width="w-40"/>
                    <IkInput label="最大值" value={value} form={form} name="kmax" width="w-40"/>
                </div>
            </div>
            <Wait wait={wait}/>
        </NuiDlg>
    );
};

export {DlgPair};
